// Decompiled by DJ v2.3.3.38 Copyright 2000 Atanas Neshkov  Date: 03/11/2007 6:40:47
// Home Page : http//members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   InformeDao.java

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.InformeDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.*;

public class InformeDao extends DAO {

    public InformeDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new InformeDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR,INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME");
            resul = prep.executeQuery();
            for (; resul.next(); c.add(getDatos(resul, new InformeDTO()))) {
                ;
            }

            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodosPorCircular(Integer integer, Integer opcion)
            throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            if (opcion.equals(0)) {
                prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME WHERE IDCIRCULAR=?");
            } else if (opcion.equals(1)) {
                prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME WHERE IDCIRCULAR=? AND ID IN(SELECT IDPLANO FROM PLANO_ASOCIADO)");
            } else if (opcion.equals(2)) {
                prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME WHERE IDCIRCULAR=? AND ID NOT IN(SELECT IDPLANOASOCIADO FROM PLANO_ASOCIADO)");
            } else if (opcion.equals(3)) {
                prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO FROM INFORME WHERE IDCIRCULAR=? AND ID IN(SELECT IDPLANOASOCIADO FROM PLANO_ASOCIADO)");
            }

            prep.setInt(1, integer.intValue());
            resul = prep.executeQuery();
            for (; resul.next(); c.add(getDatos(resul, new InformeDTO()))) {
                ;
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        InformeDTO dto = (InformeDTO) obj;
        try {
            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO INFORME(NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION,JRXML,TIPOINFORMEDEFECTO, ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("INFORME"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE INFORME SET NOMBRE=?, CONSULTA=?, IDCIRCULAR=?, INCREMENTOSUBCUENTA=?, IDCONEXION=?, JRXML=?, TIPOINFORMEDEFECTO=? WHERE ID=?");
            }
            setDatos(prep, dto, "NOMBRE, CONSULTA, IDCIRCULAR, INCREMENTOSUBCUENTA,IDCONEXION, JRXML, TIPOINFORMEDEFECTO, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM INFORME WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM INFORME WHERE NOMBRE = ?");
            prep.setString(1, ((InformeDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {            
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return (false);
    }
}
